<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="是否有效" prop="isActive">
        <el-select v-model="queryParams.isActive" placeholder="请选择是否有效" clearable size="small">
          <el-option
            v-for="dict in isActiveOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="学生id" prop="studentId">
        <el-input
          v-model="queryParams.studentId"
          placeholder="请输入学生id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="教师id" prop="teacherId">
        <el-input
          v-model="queryParams.teacherId"
          placeholder="请输入教师id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="班级id" prop="jxDeptId">
        <el-input
          v-model="queryParams.jxDeptId"
          placeholder="请输入班级id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="接送类型" prop="pickUpType">
        <el-select v-model="queryParams.pickUpType" placeholder="请选择接送类型" clearable size="small">
          <el-option
            v-for="dict in pickUpTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="接送状态" prop="pickUpState">
        <el-select v-model="queryParams.pickUpState" placeholder="请选择接送状态" clearable size="small">
          <el-option
            v-for="dict in pickUpStateOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="接送日期" prop="pickUpDate">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.pickUpDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择接送日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="家长就位" prop="pickUpReady">
        <el-select v-model="queryParams.pickUpReady" placeholder="请选择家长就位" clearable size="small">
          <el-option
            v-for="dict in pickUpReadyOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="消息触达" prop="pickUpSend">
        <el-select v-model="queryParams.pickUpSend" placeholder="请选择消息触达" clearable size="small">
          <el-option
            v-for="dict in pickUpSendOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['jx:pickup:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['jx:pickup:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['jx:pickup:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['jx:pickup:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="pickupList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="唯一ID" align="center" prop="id" />
      <el-table-column label="是否有效" align="center" prop="isActive" :formatter="isActiveFormat" />
      <el-table-column label="学生工号" align="center" prop="studentId" />
      <el-table-column label="学生姓名" align="center" prop="studentName" />
      <el-table-column label="教师工号" align="center" prop="teacherId" />
      <el-table-column label="教师姓名" align="center" prop="teacherName" />
      <el-table-column label="班级id" align="center" prop="jxDeptId" />
      <el-table-column label="班级名称" align="center" prop="jxDeptName" />
      <el-table-column label="接送类型" align="center" prop="pickUpType" :formatter="pickUpTypeFormat" />
      <el-table-column label="接送状态" align="center" prop="pickUpState" :formatter="pickUpStateFormat" />
      <el-table-column label="接送日期" align="center" prop="pickUpDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.pickUpDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="家长就位" align="center" prop="pickUpReady" :formatter="pickUpReadyFormat" />
      <el-table-column label="家长备注" align="center" prop="pickUpRemark" />
      <el-table-column label="消息触达" align="center" prop="pickUpSend" :formatter="pickUpSendFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['jx:pickup:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['jx:pickup:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学生接送对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="租户id" prop="lesseeId">
          <el-input v-model="form.lesseeId" placeholder="请输入租户id" />
        </el-form-item>
        <el-form-item label="学生id" prop="studentId">
          <el-input v-model="form.studentId" placeholder="请输入学生id" />
        </el-form-item>
        <el-form-item label="教师id" prop="teacherId">
          <el-input v-model="form.teacherId" placeholder="请输入教师id" />
        </el-form-item>
        <el-form-item label="班级id" prop="jxDeptId">
          <el-input v-model="form.jxDeptId" placeholder="请输入班级id" />
        </el-form-item>
        <el-form-item label="接送类型">
          <el-select v-model="form.pickUpType" placeholder="请选择接送类型">
            <el-option
              v-for="dict in pickUpTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="接送状态">
          <el-select v-model="form.pickUpState" placeholder="请选择接送状态">
            <el-option
              v-for="dict in pickUpStateOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="接送日期" prop="pickUpDate">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.pickUpDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择接送日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="家长就位">
          <el-select v-model="form.pickUpReady" placeholder="请选择家长就位">
            <el-option
              v-for="dict in pickUpReadyOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="家长备注" prop="pickUpRemark">
          <el-input v-model="form.pickUpRemark" placeholder="请输入家长备注" />
        </el-form-item>
        <el-form-item label="消息触达">
          <el-select v-model="form.pickUpSend" placeholder="请选择消息触达">
            <el-option
              v-for="dict in pickUpSendOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPickup, getPickup, delPickup, addPickup, updatePickup, exportPickup } from "@/api/jx/pickup";

export default {
  name: "Pickup",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 学生接送表格数据
      pickupList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否有效字典
      isActiveOptions: [],
      // 接送类型字典
      pickUpTypeOptions: [],
      // 接送状态字典
      pickUpStateOptions: [],
      // 家长就位字典
      pickUpReadyOptions: [],
      // 消息触达字典
      pickUpSendOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        isActive: undefined,
        studentId: undefined,
        teacherId: undefined,
        jxDeptId: undefined,
        pickUpType: undefined,
        pickUpState: undefined,
        pickUpDate: undefined,
        pickUpReady: undefined,
        pickUpSend: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("is_active").then(response => {
      this.isActiveOptions = response.data;
    });
    this.getDicts("JX_XSJS_TYPE").then(response => {
      this.pickUpTypeOptions = response.data;
    });
    this.getDicts("JX_XSJS_STATE").then(response => {
      this.pickUpStateOptions = response.data;
    });
    this.getDicts("JX_XSJS_READY").then(response => {
      this.pickUpReadyOptions = response.data;
    });
    this.getDicts("is_active").then(response => {
      this.pickUpSendOptions = response.data;
    });
  },
  methods: {
    /** 查询学生接送列表 */
    getList() {
      this.loading = true;
      listPickup(this.queryParams).then(response => {
        this.pickupList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 是否有效字典翻译
    isActiveFormat(row, column) {
      return this.selectDictLabel(this.isActiveOptions, row.isActive);
    },
    // 接送类型字典翻译
    pickUpTypeFormat(row, column) {
      return this.selectDictLabel(this.pickUpTypeOptions, row.pickUpType);
    },
    // 接送状态字典翻译
    pickUpStateFormat(row, column) {
      return this.selectDictLabel(this.pickUpStateOptions, row.pickUpState);
    },
    // 家长就位字典翻译
    pickUpReadyFormat(row, column) {
      return this.selectDictLabel(this.pickUpReadyOptions, row.pickUpReady);
    },
    // 消息触达字典翻译
    pickUpSendFormat(row, column) {
      return this.selectDictLabel(this.pickUpSendOptions, row.pickUpSend);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        createBy: undefined,
        createTime: undefined,
        updateTime: undefined,
        updateBy: undefined,
        isActive: undefined,
        lesseeId: undefined,
        studentId: undefined,
        teacherId: undefined,
        jxDeptId: undefined,
        pickUpType: undefined,
        pickUpState: undefined,
        pickUpDate: undefined,
        pickUpReady: undefined,
        pickUpRemark: undefined,
        pickUpSend: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学生接送";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPickup(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改学生接送";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updatePickup(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addPickup(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除学生接送编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPickup(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有学生接送数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportPickup(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
